<?php

class users_md extends CI_Model{
    private $userColum=array("fName","mInitial","IName","comment","email","password");
    private $userinfoColum= array("userBio","userCity","userState","userZip","userInterest");

    public function getAllUsers(){
        $usersQuery="SELECT * FROM user AS u JOIN userinfo AS er ON u.userId=er.userId";
        $rs= $this->db->query($usersQuery);
        return $rs->result();
   }

   public function getUserFilteredByEvent($eid){
       $usersQuery="SELECT * FROM user AS u JOIN eregistar AS er ON er.userId=u.userId JOIN userinfo as ui ON ui.userId=u.userId WHERE er.eid=$eid";
       $rs=$this->db->query($usersQuery);
       return $rs->result();
   }
   public function generateUserPath(){
       $pathStr=strtolower(base64_encode(uniqid(null,true).time()));
       $pathChuck=str_split($pathStr, 11);
       $path=implode("/", $pathChuck);
       return $path;
   }
   public function updateUserProfile($data,$userId){
       $queryStr="UPDATE user AS u LEFT JOIN userinfo AS ui ON ui.userId=u.userId SET";
       $querySec="";
       foreach($data as $key=>$value){
           if(in_array($key,$this->userinfoColum)){
               $querySec.=" ui.$key='$value',";
           }else{
               $querySec.=" u.$key='$value',";
           }
       }
       $querySec[strlen($querySec)-1]=' ';
       $finalQuery=$queryStr.$querySec." WHERE u.userId=$userId";
       return $this->db->simple_query($finalQuery);
   }
   public function getUserDatastorePath($id){
       $query="SELECT datastorePath FROM user WHERE userId=$id";
       $rs=$this->db->query($query);
       $rs=$rs->result();
        if(count($rs)>0){
            if(empty($rs[0]->datastorePath)){
                //need to make a datastore path for the user
                $userDBPath=$this->generateUserPath();
                $dbPathQuery="UPDATE user SET datastorePath='$userDBPath', hasIcon='1' WHERE userId=$id";
                $dbRs=$this->db->simple_query($dbPathQuery);
                if(!$dbRs){
                    return FALSE;
                }else{
                    return $userDBPath;
                }
            }
            return $rs[0]->datastorePath;
        }else{
            return FALSE;
        }
   }
}
?>
